package com.wskeee.ec.components.skin
{
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.components.ComboBox;
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.styles.StyleNames;
	
	import flash.display.Bitmap;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;

	public class ComboBoxSkin extends BaseSkinDrawer
	{
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		public var labelText:TextField;
		public var openButton:Button;
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		private function get combo():ComboBox
		{
			return target as ComboBox;
		}
		private function get clip():Sprite
		{
			return ui as Sprite;
		}
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  init
		//-----------------------------------------------------------------
		override public function init(target:EComponent, ui:*):void
		{
			super.init(target,ui);
			openButton = new Button();
			openButton.label = "";
			labelText = new TextField();
			labelText.type = TextFieldType.INPUT;
			labelText.multiline = false;
			labelText.height = 20;
			labelText.selectable = labelText.mouseEnabled = false;
			clip.addChild(openButton);
			clip.addChild(labelText);
		}
		//-----------------------------------------------------------------
		//  redraw
		//-----------------------------------------------------------------
		override public function redraw():void
		{
			var skins:Vector.<Object> = new <Object> [getSkin("upSkin"),getSkin("overSkin"),getSkin("downSkin")];
			if(!skins[0])skins = drawOpenButtonSkin();
			openButton.upSkin = skins[0];
			openButton.overSkin = skins[1];
			openButton.downSkin = skins[2];
		}
		
		private function drawOpenButtonSkin():Vector.<Object>
		{
			var vs:Vector.<Object> = new Vector.<Object>(3);
			var skin:Sprite;
			var colors:Array = [getStyle("buttonUpColor"),getStyle("buttonOverColor"),getStyle("buttonDownColor")];
			var fillAlpha:Number = getStyle("buttonFillAlpha") as Number;
			var bm:Bitmap;
			var arrowPoints:Vector.<Number> = new <Number>[86,8.5, 96,8.5, 91,13.5];
			for(var i:int=0;i<3;i++)
			{
				skin = new Sprite();
				skin.graphics.lineStyle(1,getStyle("borderColor")as uint,1,true,LineScaleMode.NONE);
				skin.graphics.beginFill(colors[i],fillAlpha);
				skin.graphics.drawRoundRect(0,0,100,22,5,5);
				skin.graphics.endFill();
				skin.graphics.drawPath(new <int>[1,2],new <Number>[80,0, 80,22]);
				skin.graphics.endFill();
				
				skin.graphics.lineStyle(undefined);
				skin.graphics.beginFill(getStyle(StyleNames.BUTTON_DOWN_COLOR) as uint,0.8);
				skin.graphics.drawPath(new <int>[1,2,2],arrowPoints);
				
				skin.scale9Grid = new Rectangle(5,5,69,12);
				vs[i] = skin;
			}
			
			return vs;
		}
		//-----------------------------------------------------------------
		//  validateProperties
		//-----------------------------------------------------------------
		override public function validateProperties():void
		{
			openButton.selected = combo.isOpen;
		}
		//-----------------------------------------------------------------
		//  validateSize
		//-----------------------------------------------------------------
		//-----------------------------------------------------------------
		//  validateDisplayList
		//-----------------------------------------------------------------
		override public function validateDisplayList():void
		{
			openButton.width = combo.width;
			openButton.height = combo.height;
			labelText.width = combo.width - 22;
			labelText.height = combo.height;
			labelText.x= labelText.y = 0;
		}
	}
}